package inglar.autotc.shared.db;

import java.io.Serializable;

public class Templates implements Serializable {

	private static final long serialVersionUID = -8980914093935816599L;
	
	private String id = null;
	private String name = null;
	private String device = null;
	private String script = null;
	
	public static final String head = "templates@{";
	public static final String tail = "}@";
	public static final String fieldSeparator = "#";
	
	public Templates() {
		
	}
	
	public Templates(String string) {
		this();
		
		String functag = "Templates(String)";
		int numfields = 4;
		
		if(string == null) 
			throw new NullPointerException(functag + ": Argument 1 is null");
		
		string = string.trim();
		if(!string.startsWith(head) || !string.endsWith(tail)) 
			throw new IllegalArgumentException(functag + ": Argument 1 is not " +
					"wrapped with '" + head + "' and '" + tail + "'");
		
		string = string.substring(head.length(), string.indexOf(tail));
		
		String[] values = string.split(fieldSeparator);
		
		if(values.length != numfields) 
			throw new IllegalArgumentException(functag + ": The number of " +
					"fields is not " + numfields);
		
		try {
			this.setId(values[0]);
			this.setName(values[1]);
			this.setDevice(values[2]);
			this.setScript(values[3]);
		} catch(Throwable cause) {
			throw new IllegalArgumentException(functag + ": " + 
					cause.getMessage());
		}
	}
	
	public String getId() {
		return id;
	}

	public void setId(String id) throws IllegalArgumentException {
		try {
			this.id = (new Integer(id)).toString();
		} catch (NumberFormatException nfe) {
			throw new IllegalArgumentException("id must be an integer");
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if(name == null) throw new IllegalArgumentException("name must not be null");
		name = name.trim();
		if(name.isEmpty()) throw new IllegalArgumentException("name must not be empty");
		this.name = name;
	}

	public String getDevice() {
		return device;
	}

	public void setDevice(String device) {
		if(device == null) throw new IllegalArgumentException("device must not be null");
		device = device.trim();
		if(device.isEmpty()) throw new IllegalArgumentException("device must not be empty");
		this.device = device;
	}

	public String getScript() {
		return script;
	}

	public void setScript(String script) {
		if(script == null) throw new IllegalArgumentException("script must not be null");
		script = script.trim();
		if(script.isEmpty()) throw new IllegalArgumentException("script must not be empty");
		this.script = script;
	}
	
	public String toString() {
		return head + 
				this.getId() + fieldSeparator + 
				this.getName() + fieldSeparator +
				this.getDevice() + fieldSeparator + 
				this.getScript() + 
				tail;
	}

	/**
	 * Used for testing.
	 */
	public static void main(String[] args) {
		System.out.println(Templates.class.getName());
		
		try {
			
			Templates t = new Templates();
			System.out.println(t);
			
		} catch (Throwable caught) {
			caught.printStackTrace();
		}
	}

}
